package com.mongodb.document.geoJson;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.mongodb.constants.GeoJsonConstants;
import com.mongodb.utils.GeoJsonUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.geo.GeoJson;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;

/**
 * 包装Geometry
 */
@Data
@NoArgsConstructor
@ApiModel("编辑时包装的geometry")
public class WrappedGeometry implements Serializable {
    private static final long serialVersionUID = -1604811141066873917L;
    @ApiModelProperty(value = "WrappedGeometry对象id")
    private String gId;
    @ApiModelProperty(value = "包装Geometry属性")
    @Field("properties")
    private WrappedPropertyBuilder properties;
    @ApiModelProperty(value = "(输入)GeoJson地理位置信息")
    @Transient
    private JSONObject geometryIn;
    @ApiModelProperty(hidden = true, value = "(不返给前端)GeoJson地理位置信息(因为geoJson用了Transient所以geometry必须使用初始值)")
    @JSONField(serialize = false)
    private GeoJson geometry;

    public void setGeometryIn(JSONObject geometryIn) {
        this.geometryIn = geometryIn;
        if (geometryIn != null && StringUtils.isNotBlank((String) geometryIn.get(GeoJsonConstants.TYPE))) {
            this.geometry = GeoJsonUtils.newGeoJson(geometryIn);
        } else {
            this.geometry = null;
        }
    }

    public JSONObject getGeometryIn() {
        return GeoJsonUtils.parseGeoJsonToJSONObject(getGeometry());
    }
}
